<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title><s:if test="hotKeyword.hotKeywordId==null"><s:text name="hotKeyword.add.title"/></s:if><s:else><s:text name="hotKeyword.edit.title"/></s:else></title>
	<%@ include file="/common/meta.jsp"%>
	<%@ include file="/common/common.jsp"%>
</head>
<body>
<div id="wrapper">
	<div class="curmb"><s:text name="your.site.position"/>:<a href="${ctx}/main.action"><s:text name="homepage"/></a><span>></span><s:text name="hotKeyword.title"/></div>
	<div class="listtit"><p><span><strong><s:if test="hotKeyword.hotKeywordId==null"><s:text name="hotKeyword.add.title"/></s:if><s:else><s:text name="hotKeyword.edit.title"/></s:else></strong></span></p></div>
	<div class="listadd">
		<s:form action="hotkeyword-save" onsubmit="return chkform()">
		<s:hidden name="hotKeyword.hotKeywordId" id="hotKeywordId"/>
		
			<table class="addgrid">
				<%-- <tr>
					<th><s:text name="hotKeyword.searchNum"/>:</th>
					<td><s:textfield name="hotKeyword.searchNum" required="true" requiredposition="right" cssClass="txtin"/></td>
				</tr> --%>
				<%-- <tr>
					<th><s:text name="hotKeyword.isGenerate"/>:</th>
					<td><s:textfield name="hotKeyword.isGenerate" required="true" requiredposition="right" cssClass="txtin"/></td>
				</tr> --%>
				<s:if test="orig.equals(\"edit\")">
					<tr>
						<td>
							<s:text name="hotKeyword.hotType"/>:
							<s:property value="%{transForm()}"/>
						</td>
					</tr>
					
				</s:if>
				<s:else>
					<tr>
						<td>
							<s:if test="%{authLicence('1_1_8')}">
								<input type="button" value="增加关键字" class="btnlist4" onclick="addOption();"/><span style="color:red" id="other-message"></span>
							</s:if>
							<s:else>
								<input type="button" value="增加关键字" class="btnlist4" onclick="addOption();" disabled="disabled"/>
							</s:else>
								<span style="color:red" id="hotKeyword-messagesyz"></span>
						</td>
					
					</tr>
				</s:else>
				<tr>
					<td style="width: 900px;">
						<ul id="addOpt">
							<li>
								关键字:
								<s:textfield name="hotKeyword.hotKeywordName" cssClass="txtin"  sign="no" onblur="checkKeyword(this); " onfocus="changeSign(this);"/>
								<s:text name="hotKeyword.frontFilename"/>:
								<s:textfield name="hotKeyword.frontFilename" required="true" requiredposition="right" cssClass="txtin" sign="no" onblur="frontword(this);" onfocus="changeSign(this);"/>
								<s:if test="orig.equals(\"input\")">
									<a href="#" class="del"><s:text name="delete"/></a>
								</s:if>
								<span style="color:red" ></span>
								<span style="color:red" ></span>
							</li>
						</ul>
					</td>
				
				</tr>
				<tr>
					
					<td>
						<s:if test="%{authLicence('1_1_8')}">
							<s:submit value="%{getText('save')}" cssClass="btnlist2" id="keywordSave"/>
						</s:if>
						<s:else>
							<s:submit value="%{getText('save')}" cssClass="btnlist2" id="keywordSave" disabled="true"/>
						</s:else>
						<s:reset value="%{getText('reset')}" cssClass="btnlist2" onclick="cleanSign()"/>
						<s:reset value="%{getText('返回')}" onclick="history.go(-1);" cssClass="btnlist2"/>
					</td>
				</tr>
			</table>
		</s:form>
	</div>
</div>
</body>
</html>
<script type="text/javascript" defer="defer">
	var hotKeywordId = $("#hotKeywordId").val();

	function chkform(){
		if(submitCheck()){
			$("form[name='hotKeywordhotKeywordId']").submit();
		}else{return false};
	}
	
	function submitCheck(){
		var flag = true;
		$("li").each(function(i,item){
				if($(item).find("input:eq(0)").attr("sign") == "no"){
					checkKeyword($(item).find("input:eq(0)"));
					flag = false;
				}else if($(item).find("input:eq(0)").attr("sign") == "dpass"){
					flag = false;
				}else{
				}
				if($(item).find("input:eq(1)").attr("sign") == "no"){
					frontword($(item).find("input:eq(1)"));
					flag = false;
				}else if($(item).find("input:eq(1)").attr("sign") == "dpass"){
					flag = false;
				}else {
				}
		});
		return flag;
	}
	function cleanSign(){
		$("#addOpt").find("input").attr("sign","no");
		$("#addOpt").find("span").html("");
	}
	function addOption(){
		$("#addOpt").append($("#addOpt").find("li:eq(0)").clone());
		$("#addOpt").find("li:last input").each(function(){$(this).val("");$(this).attr("sign","no")});
		$("#addOpt").find("li:last span").html("");
		$("#addOpt").find("li:last a").attr("href","#this");
		goDel();
	}
	function goDel(){
		$(".del").each(function(index){
			$(this).click(function(){
				if(index != 0){
					if(hotKeywordId == null || hotKeywordId == ""){
						
					}else {
						
					}
					$(this).parent().remove();
				}else{
					$("#addOpt").find("li:eq(0) span").first().html("(第一个不能删除)");
				}
			});
		});
	}
	function checkUniq1(event){
		var count = 0;
		$("li").each(function(i,item){
			if($(event).val()==$(item).find("input:eq(0)").val()){
				count++;
			}
		});
		if(count==1){
		  return true;
		}else{
			return false;
		}
	}
	function checkUniq2(event){
		var count = 0;
		$("li").each(function(i,item){
			if($(event).val()==$(item).find("input:eq(1)").val()){
				count++;
			}
		});
		if(count==1){
		  return true;
		}else{
			return false;
		}
	}
	function checkKeyword(event){
		var keyword = $(event).val();
		if(keyword=="") {
			 $(event).parent().children().last().prev().html("(关键字不能为空)");
			 $(event).attr("sign","dpass");
		}else{
			if(!checkUniq1($(event))){
				 $(event).parent().children().last().prev().html("(该关键字与其他关键字重名)");
				 $(event).attr("sign","dpass");
			}else{
				var obj = {"hotKeyword.hotKeywordName":keyword, "hotKeyword.frontFilename":"", "hotKeyword.hotKeywordId":hotKeywordId};
				$.post("${ctx}/search/hotkeyword-checkkeyword.action", obj, function(data){
					if(data=="keywordNo"){
						 $(event).parent().children().last().prev().html("(该关键字已经存在)");
						 $(event).attr("sign","dpass");
					}else if(data=="keywordYes"){
						$(event).parent().children().last().prev().html("");
						 $(event).attr("sign","pass");
					}else{
						
					}
				});
			}
		}
	}
	
	function frontword(event){
		var frontword = $(event).val();
		if(frontword=="") {
			 $(event).parent().children().last().html("(文件前缀不能为空)");
			 $(event).attr("sign","dpass");
		}else{
			if(!checkUniq2($(event))){
				 $(event).parent().children().last().html("(文件前缀与其他前缀重名)");
				 $(event).attr("sign","dpass");
			}else{
				var obj = {"hotKeyword.hotKeywordName":"", "hotKeyword.frontFilename":frontword, "hotKeyword.hotKeywordId":hotKeywordId};
				$.post("${ctx}/search/hotkeyword-checkkeyword.action", obj, function(data){
					if(data=="frontFilenameNo"){
						 $(event).parent().children().last().html("(文件前缀已经存在)");
						 $(event).attr("sign","dpass");
					}else if(data=="frontFilenameYes"){
						$(event).parent().children().last().html("");
						 $(event).attr("sign","pass");
					}else{
						
					}
				});
			}
		}
	}
	
	function changeSign(event){
		$(event).attr("sign","no");
	}
	
	
	
	
</script>